package cn.yunyichina.provider.message.service.impl;

import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.iface.entity.ErrorCodeDefine;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.message.AppMessageVo;
import cn.yunyichina.provider.iface.entity.message.SendSmsVo;
import cn.yunyichina.provider.message.dao.MsgTemplateDao;
import cn.yunyichina.provider.message.dao.OrgAppTemplateDao;
import cn.yunyichina.provider.message.thread.MessagePushThreadPool;
import cn.yunyichina.provider.message.thread.MsgAppPushRunnable;
import cn.yunyichina.provider.message.thread.app.MsgImgAppPushRunnable;
import cn.yunyichina.provider.message.thread.app.MsgTextAppPushRunnable;
import cn.yunyichina.provider.message.thread.app.MsgTmpAppPushRunnable;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.log.Log;
import cn.yunyichina.utils.log.constant.LogParamType;
import cn.yunyichina.utils.log.constant.LogScene;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * App消息推送 Created by Administrator on 2016/9/9.
 */
@Service
public class MsgAppServiceImpl {
	private static Logger logger = LoggerFactory.getLogger(MsgAppServiceImpl.class);

	@Autowired
	MsgTemplateDao msgTemplateDao;

	@Autowired
	MsgCenterServiceImpl msgCenterService;

	@Autowired
	OrgAppTemplateDao orgAppTemplateDao;

	@Autowired
	MsgSmsServiceImpl msgSmsServicel;

	@Autowired
	MsgServiceImpl msgService;

	/**
	 * App消息推送 接口名称 yyt.message.msg.app.send
	 * 
	 * @param strJson
	 *            org_app_code,org_code,to_user,template_code,msg_content
	 * @return
	 */
	public ResponseObject sendMsgApp(String strJson) {
		logger.debug("sendMsgApp:" + strJson);
		ResponseObject response = new ResponseObject();

		// Map dataMap = JSON.parseObject(data,Map.class);
		// logger.info("sendMsgApp Params===="+JSON.toJSONString(dataMap));
		// String userType = (String) dataMap.get("user_type");
		// String toUser = (String) dataMap.get("to_user");
		// String templateCode = (String) dataMap.get("template_code");
		// Map<String,Object> contentMap =
		// JsonUtils.parseObject(JsonUtils.toJsonString(dataMap.get("msg_content")),Map.class);
		// contentMap = JsonUtils.mapKeyTranslate(contentMap,true,true);
		
		
		MessagePushThreadPool.messageThreadPool.execute(new MsgAppPushRunnable(strJson));
		
		response.setResultCodeAndMessage("0", "成功");
		
		return response;
	}



	private void validParam(JSONObject params, String fieldNames) {
		if (!StringUtils.isEmpty(fieldNames)) {
			String[] arrFieldName = fieldNames.split(",");
			for (String fieldName : arrFieldName) {
				if (StringUtils.isEmpty(params.getString(fieldName.trim()))) {
					throw new CallException(ErrorCodeDefine.MESSAGE_PARAM, "参数异常" + fieldName.trim());
				}
			}
		}
	}


	/*==============================================================================*/


	/**
	 * App消息推送(重构)
	 * @param appMessageVo
	 * @interfaceName: yyt.message.msg.app.god.send
	 * @return
	 */
	public ResponseObject sendMsgAppGod(AppMessageVo appMessageVo) {
		Log.i(LogScene.MSG,"yyt.message.msg.app.god.send", LogParamType.REQUEST,appMessageVo.getOpenId(), JsonUtils.toJsonString(appMessageVo));
		ResponseObject responseObject = new ResponseObject();
		Short templateType = appMessageVo.getLibraryTemplateType();
		Short platformType = appMessageVo.getPlatformType();

		//根据类型分发线程
		if(platformType == 4){
			SendSmsVo sendSmsVo = new SendSmsVo();
			sendSmsVo.setTemplateCode(appMessageVo.getPlatformTemplateCode());
			sendSmsVo.setPlatformOrgAppCode(appMessageVo.getPlatformOrgAppCode());
			sendSmsVo.setOrgCode(appMessageVo.getDevOrgCode());
			sendSmsVo.setMsgContentMap(appMessageVo.getMsgContentMap());
			sendSmsVo.setMobileNo(appMessageVo.getMobileNo());
			sendSmsVo.setSmsCode(appMessageVo.getSmsCode());
			sendSmsVo.setSmsSignName(appMessageVo.getSmsSignName());
			responseObject = msgSmsServicel.sendMsgSmsGod(sendSmsVo);
			Log.i(LogScene.MSG,"yyt.message.msg.app.god.send", LogParamType.RESPONSE,appMessageVo.getOpenId(), JsonUtils.toJsonString(responseObject));
		}else {
			switch (templateType){
				case 1 : MessagePushThreadPool.messageThreadPool.execute(new MsgTextAppPushRunnable(appMessageVo , responseObject));  //文本
					break;
				case 2 : MessagePushThreadPool.messageThreadPool.execute(new MsgImgAppPushRunnable(appMessageVo , responseObject));  //图文
					break;
				case 3 : MessagePushThreadPool.messageThreadPool.execute(new MsgTmpAppPushRunnable(appMessageVo , responseObject));  //模板
					break;
				case 4 : sendJPush(appMessageVo);  //极光推送
					break;
				default:
					Log.i(LogScene.MSG,"yyt.message.msg.app.god.send", LogParamType.NORMAL,appMessageVo.getOpenId(),"[MsgAppServiceImpl.sendMsgAppGod] 根据传入参数无法匹配到模板类型 LibraryTemplateType：" + templateType);
					responseObject.setResultCodeAndMessage("8200" ,"[MsgAppServiceImpl.sendMsgAppGod] 根据传入参数无法匹配到模板类型 LibraryTemplateType：" + templateType);
			}
			Log.i(LogScene.MSG,"yyt.message.msg.app.god.send", LogParamType.RESPONSE,appMessageVo.getOpenId(), JsonUtils.toJsonString(responseObject));
		}

		return responseObject;
	}



	/**
	 * 极光推送构造
	 * @param appMessageVo
	 */
	public void sendJPush(AppMessageVo appMessageVo) {

		String paramMaps = JSON.toJSONString(appMessageVo.getMsgContentMap());
		Log.i(LogScene.MSG_JPUSH,"yyt.message.msg.sms.god.send", LogParamType.NORMAL,appMessageVo.getOpenId(),"解析极光推送msgContent：" + paramMaps);

		//根据传入实体构造json体
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("user_type" , appMessageVo.getUserType());
		jsonObject.put("order_no" , appMessageVo.getOrderNo());
		jsonObject.put("org_app_code" , appMessageVo.getPlatformOrgAppCode());
		jsonObject.put("url" , appMessageVo.getUrl());
		jsonObject.put("org_code" , appMessageVo.getDevOrgCode());
		jsonObject.put("platform_type" , appMessageVo.getPlatformType());
		jsonObject.put("hospital_code" , appMessageVo.getDevOrgCode());
		jsonObject.put("to_user" , appMessageVo.getOpenId());
		jsonObject.put("template_code" , appMessageVo.getPlatformTemplateCode());
		jsonObject.put("sms_msg_content" , paramMaps);
		Log.i(LogScene.MSG_JPUSH,"yyt.message.msg.sms.god.send", LogParamType.NORMAL,appMessageVo.getOpenId(),"最终构造极光推送请求体：" + jsonObject.toJSONString());
		msgService.sendMsgTemplateApp(jsonObject.toJSONString());

	}

}
